﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Smart
{
    public enum Settings
    {
        ServiceUrl,
        Sunucu,
        Veritabani,
        OturumSekli,
        VtKullaniciAdi,
        VtSifre,
        SrmMerkez,
        FirmaNo,
        SubeNo,
        MikroKullanici,
        PlasiyerKodu,
        DovizliKullanim,
        KendiEvrakDegistir,
        BaskaEvrakDegistir,
        Depo,
        KullaniciId,
        BirinciBirimAktif,
        BelgeGirisZorunlu,
        TanimsizBarkod,
        SyncTime,
        SaglayicidanMalKabulSerisi,
        SaglayiciyaIadeSeri,
        MusteriyeSatisEvrakSeri,
        MusteridenIadeAlisSeri,
        AlinanSiparisEvrakSeri,
        VerilenSiparisSeri,
        SubeSiparisEvrakSeri,
        GelenSevkEvrakSeri,
        GidenDepoSevkSeri,
        Srm,
        TahsilatNakitSeri,
        TahsilatCekSeri,
        TahsilatSenetSeri,
        TahsilatKrediKartiSeri,
        TahsilatNakitKasaKodu,
        TahsilatCekKasaKodu,
        TahsilatSenetKasaKodu,
        TahsilatKrediKartiBankaKodu

    }

    public class Sessions
    {
        private Sessions() { _sessions = new Dictionary<string, object>(); }
        private static Sessions _instance;

        public int VarsayilanDepo()
        {
            int depoNo = 0;
            string depo = this[Settings.Depo].ToString();
            if (depo.IndexOf('-') > -1)
            {
                depoNo = Cevir.Parse<int>(depo.Split('-')[0]);
            }
            else
            {
                depoNo = Cevir.Parse<int>(depo);
            }
            return depoNo;
        }
        public int[] KullanilabilirDepolar()
        {
            List<int> depolar = new List<int>();
            string depo = this[Settings.Depo].ToString();
            if (depo.IndexOf('-') > -1)
            {
                foreach (string item in depo.Split('-'))
                {
                    depolar.Add(Cevir.Parse<int>(item));
                }
            }
            else
            {
                depolar.Add(Cevir.Parse<int>(depo));
            }
            return depolar.ToArray();
        }

        public string VarsayilanSrm()
        {
            string srmNo = "0";
            string srm = this[Settings.Srm].ToString();
            if (srm.IndexOf('-') > -1)
            {
                srmNo = srm.Split('-')[0];
            }
            else
            {
                srmNo = srm;
            }
            return srmNo;
        }
        public string[] KullanilabilirSrm()
        {
            List<string> srm = new List<string>();
            string prm = this[Settings.Srm].ToString();
            if (prm.IndexOf('-') > -1)
            {
                foreach (string item in prm.Split('-'))
                {
                    srm.Add(item);
                }
            }
            else
            {
                srm.Add(prm);
            }
            return srm.ToArray();
        }

        public static Sessions CreateSession
        {
            get
            {
                if (_instance == null)
                    _instance = new Sessions();
                _instance.readAllSettings();
                return _instance;
            }
        }

        public void CloseSession()
        {
            saveAllSettings();
        }

        private Dictionary<string, object> _sessions;

        public object this[string key]
        {
            get
            {
                if (_sessions.ContainsKey(key))
                    return _sessions[key];
                else
                    return "";
            }
            set
            {
                if (_sessions.ContainsKey(key))
                    _sessions[key] = value;
                else
                    _sessions.Add(key, value);
            }
        }

        public object this[Settings key]
        {
            get
            {
                if (_sessions.ContainsKey(key.ToString()))
                    return _sessions[key.ToString()];
                else
                    return "";
            }
            set
            {
                if (_sessions.ContainsKey(key.ToString()))
                    _sessions[key.ToString()] = value;
                else
                    _sessions.Add(key.ToString(), value);
            }
        }

        private void readAllSettings()
        {
            string filePath = Methods.StartupPath() + "\\ayarlar.ini";
            if (File.Exists(filePath))
            {
                using (StreamReader sr = new StreamReader(filePath))
                {
                    string line;
                    string[] fields;
                    while (sr.Peek() > -1)
                    {
                        line = sr.ReadLine();
                        fields = line.Split(';');
                        if (fields[1].IndexOf('-') > -1 && fields[1].IndexOf(':') > -1)
                            _sessions.Add(fields[0], fields[1].ToFullDateTime());
                        else
                            _sessions.Add(fields[0], fields[1]);
                    }
                    sr.Close();
                }
            }
        }

        private void saveAllSettings()
        {
            string filePath = Methods.StartupPath() + "\\ayarlar.ini";
            using (StreamWriter sw = new StreamWriter(filePath, false))
            {
                foreach (KeyValuePair<string, object> item in _sessions)
                {
                    if (item.Value is DateTime)
                        sw.WriteLine(string.Format("{0};{1}", item.Key, ((DateTime)item.Value).ToFullSqlString()));
                    else
                        sw.WriteLine(string.Format("{0};{1}", item.Key, item.Value));
                }
                sw.Close();
            }
        }

    }
}
